package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectCreditStat;
import com.shrct.businessprocess.project.RCProjectCreditStatShouldRequestInfo;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCRealMoney;
import com.shrct.businessprocess.project.impl.RCProjectImpl;
import com.shrct.businessprocess.project.proxy.RCProjectCreditStatProxy;
import com.shrct.businessprocess.purchase.RCPurchaseIncomeRequest;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCPROJECTCREDITSTAT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcprojectcreditstat")
public class RCProjectCreditStatImpl extends BaseModelObject implements RCProjectCreditStat {
  public RCProjectCreditStatImpl() {
    super();
  }
  
  @JoinColumn(name = "project_id")
  @ManyToOne(targetEntity = RCProjectImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCProject project;
  
  public RCProject getProject() {
    return this.project;
  }
  
  public RCProjectCreditStat setProject(final RCProject project) {
    this.project = project;
    return this;			
    
  }
  
  private Date startDate;
  
  public Date getStartDate() {
    return this.startDate;
  }
  
  public RCProjectCreditStat setStartDate(final Date startDate) {
    this.startDate = startDate;
    return this;			
    
  }
  
  @JoinColumn(name = "majorSaler_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee majorSaler;
  
  public RCEmployee getMajorSaler() {
    return this.majorSaler;
  }
  
  public RCProjectCreditStat setMajorSaler(final RCEmployee majorSaler) {
    this.majorSaler = majorSaler;
    return this;			
    
  }
  
  private Integer year;
  
  public Integer getYear() {
    return this.year;
  }
  
  public RCProjectCreditStat setYear(final Integer year) {
    this.year = year;
    return this;			
    
  }
  
  private Boolean isInitializedAsEB;
  
  public Boolean getIsInitializedAsEB() {
    return this.isInitializedAsEB;
  }
  
  public RCProjectCreditStat setIsInitializedAsEB(final Boolean isInitializedAsEB) {
    this.isInitializedAsEB = isInitializedAsEB;
    return this;			
    
  }
  
  private Date ebCancelDate = null;
  
  public Date getEbCancelDate() {
    return this.ebCancelDate;
  }
  
  public RCProjectCreditStat setEbCancelDate(final Date ebCancelDate) {
    this.ebCancelDate = ebCancelDate;
    return this;			
    
  }
  
  private Boolean isCurrentEB;
  
  public Boolean getIsCurrentEB() {
    return this.isCurrentEB;
  }
  
  public RCProjectCreditStat setIsCurrentEB(final Boolean isCurrentEB) {
    this.isCurrentEB = isCurrentEB;
    return this;			
    
  }
  
  private Boolean isBadProject = Boolean.valueOf(false);
  
  public Boolean getIsBadProject() {
    return this.isBadProject;
  }
  
  public RCProjectCreditStat setIsBadProject(final Boolean isBadProject) {
    this.isBadProject = isBadProject;
    return this;			
    
  }
  
  public Double getContractAmount() {
    RCProjectMoney _totalContractAmount = this.project.getTotalContractAmount();
    Double _hostAmount = _totalContractAmount.toHostAmount();
    return _hostAmount;
  }
  
  public Integer getEbDays() {
    int _xblockexpression = (int) 0;
    {
      int _xifexpression = (int) 0;
      boolean _not = (!this.isInitializedAsEB);
      if (_not) {
        _xifexpression = 0;
      } else {
        int _xblockexpression_1 = (int) 0;
        {
          Date _xifexpression_1 = null;
          if ((this.isCurrentEB).booleanValue()) {
            Date _day = DateUtil.today();
            _xifexpression_1 = _day;
          } else {
            _xifexpression_1 = this.ebCancelDate;
          }
          final Date ebCountDate = _xifexpression_1;
          Long _between = DateUtil.between(this.startDate, ebCountDate);
          int _intValue = _between.intValue();
          _xblockexpression_1 = (_intValue);
        }
        _xifexpression = _xblockexpression_1;
      }
      final int result = _xifexpression;
      _xblockexpression = (result);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  public Double getNoInvoiceAmount() {
    return Double.valueOf(0d);
  }
  
  public RCProjectCreditStatShouldRequestInfo getShouldRequestInfo() {
    RCProjectCreditStatShouldRequestInfo _rCProjectCreditStatShouldRequestInfo = new RCProjectCreditStatShouldRequestInfo();
    final Procedure1<RCProjectCreditStatShouldRequestInfo> _function = new Procedure1<RCProjectCreditStatShouldRequestInfo>() {
        public void apply(final RCProjectCreditStatShouldRequestInfo info) {
          Collection<RCCustomerContract> _contracts = RCProjectCreditStatImpl.this.project.getContracts();
          final Function1<RCCustomerContract,RCProjectMoney> _function = new Function1<RCCustomerContract,RCProjectMoney>() {
              public RCProjectMoney apply(final RCCustomerContract it) {
                RCProjectMoney _totalInvoiceAmount = it.getTotalInvoiceAmount();
                return _totalInvoiceAmount;
              }
            };
          Iterable<RCProjectMoney> _map = IterableExtensions.<RCCustomerContract, RCProjectMoney>map(_contracts, _function);
          RCExchangeRate _defaultExchangeRate = RCProjectCreditStatImpl.this.project.getDefaultExchangeRate();
          RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
          Double _hostAmount = _sum.toHostAmount();
          final Double totalInvoice = NumberExtensions.nullsafe(_hostAmount);
          Collection<RCCustomerContract> _contracts_1 = RCProjectCreditStatImpl.this.project.getContracts();
          final Function1<RCCustomerContract,RCRealMoney> _function_1 = new Function1<RCCustomerContract,RCRealMoney>() {
              public RCRealMoney apply(final RCCustomerContract it) {
                RCRealMoney _totalAcceptedAmount = it.getTotalAcceptedAmount();
                return _totalAcceptedAmount;
              }
            };
          Iterable<RCRealMoney> _map_1 = IterableExtensions.<RCCustomerContract, RCRealMoney>map(_contracts_1, _function_1);
          RCExchangeRate _defaultExchangeRate_1 = RCProjectCreditStatImpl.this.project.getDefaultExchangeRate();
          RCRealMoney _sumRealMoney = Moneys.sumRealMoney(_map_1, _defaultExchangeRate_1);
          Double _hostAmount_1 = _sumRealMoney.toHostAmount();
          final Double totalIncome = NumberExtensions.nullsafe(_hostAmount_1);
          double _minus = DoubleExtensions.operator_minus(totalInvoice, totalIncome);
          info.setShouldRequestAmount(Double.valueOf(_minus));
          IGenericQuery<RCPurchaseIncomeRequest> query=getObjectFactory().createGenericQuery(RCPurchaseIncomeRequest.class,"select e from com.shrct.businessprocess.purchase.impl.RCPurchaseIncomeRequestImpl as e   where ((e.project) = (:project)) and  e.active = 1      ").setParameter("project",project);
          Collection<RCPurchaseIncomeRequest> _list = query.list();
          final Function1<RCPurchaseIncomeRequest,Boolean> _function_2 = new Function1<RCPurchaseIncomeRequest,Boolean>() {
              public Boolean apply(final RCPurchaseIncomeRequest it) {
                boolean _and = false;
                Boolean _isPassed = it.getIsPassed();
                if (!(_isPassed).booleanValue()) {
                  _and = false;
                } else {
                  Boolean _allPaid = it.getAllPaid();
                  boolean _not = (!_allPaid);
                  _and = ((_isPassed).booleanValue() && _not);
                }
                return Boolean.valueOf(_and);
              }
            };
          Iterable<RCPurchaseIncomeRequest> _filter = IterableExtensions.<RCPurchaseIncomeRequest>filter(_list, _function_2);
          final Procedure1<RCPurchaseIncomeRequest> _function_3 = new Procedure1<RCPurchaseIncomeRequest>() {
              public void apply(final RCPurchaseIncomeRequest request) {
                Date _passDate = request.getPassDate();
                Date _day = DateUtil.today();
                final Long days = DateUtil.between(_passDate, _day);
                boolean _lessEqualsThan = ((days).longValue() <= 90);
                if (_lessEqualsThan) {
                  Double _amount90 = info.getAmount90();
                  Double _remainAmount = request.getRemainAmount();
                  double _plus = DoubleExtensions.operator_plus(_amount90, _remainAmount);
                  info.setAmount90(Double.valueOf(_plus));
                } else {
                  boolean _and = false;
                  boolean _greaterThan = ((days).longValue() > 90);
                  if (!_greaterThan) {
                    _and = false;
                  } else {
                    boolean _lessEqualsThan_1 = ((days).longValue() <= 180);
                    _and = (_greaterThan && _lessEqualsThan_1);
                  }
                  if (_and) {
                    Double _amount91_180 = info.getAmount91_180();
                    Double _remainAmount_1 = request.getRemainAmount();
                    double _plus_1 = DoubleExtensions.operator_plus(_amount91_180, _remainAmount_1);
                    info.setAmount91_180(Double.valueOf(_plus_1));
                  } else {
                    boolean _and_1 = false;
                    boolean _greaterThan_1 = ((days).longValue() > 180);
                    if (!_greaterThan_1) {
                      _and_1 = false;
                    } else {
                      boolean _lessEqualsThan_2 = ((days).longValue() <= 1860);
                      _and_1 = (_greaterThan_1 && _lessEqualsThan_2);
                    }
                    if (_and_1) {
                      Double _amount181_1860 = info.getAmount181_1860();
                      Double _remainAmount_2 = request.getRemainAmount();
                      double _plus_2 = DoubleExtensions.operator_plus(_amount181_1860, _remainAmount_2);
                      info.setAmount181_1860(Double.valueOf(_plus_2));
                    } else {
                      Double _amount1861 = info.getAmount1861();
                      Double _remainAmount_3 = request.getRemainAmount();
                      double _plus_3 = DoubleExtensions.operator_plus(_amount1861, _remainAmount_3);
                      info.setAmount1861(Double.valueOf(_plus_3));
                    }
                  }
                }
              }
            };
          IterableExtensions.<RCPurchaseIncomeRequest>forEach(_filter, _function_3);
        }
      };
    RCProjectCreditStatShouldRequestInfo _doubleArrow = ObjectExtensions.<RCProjectCreditStatShouldRequestInfo>operator_doubleArrow(_rCProjectCreditStatShouldRequestInfo, _function);
    return _doubleArrow;
  }
  
  public static RCProjectCreditStat create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCProjectCreditStat rCProjectCreditStat = new com.shrct.businessprocess.project.impl.RCProjectCreditStatImpl(
    );
    objectFactory.create(rCProjectCreditStat);
    return rCProjectCreditStat;			
    
  }
  
  public static RCProjectCreditStat createRCProjectCreditStatImpl(final IObjectFactory objectFactory, final Procedure1<RCProjectCreditStat> init) {
    com.shrct.businessprocess.project.RCProjectCreditStat rCProjectCreditStat = new com.shrct.businessprocess.project.impl.RCProjectCreditStatImpl(
    );
    objectFactory.create(rCProjectCreditStat);
    init.apply(rCProjectCreditStat);
    
    return rCProjectCreditStat;			
    
  }
  
  public RCProjectCreditStatProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCProjectCreditStatProxy proxy = new com.shrct.businessprocess.project.proxy.RCProjectCreditStatProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
